<template>
  <div class="songList">
    <div class="handle-box">
      <el-input v-model="paramObj.name" placeholder="歌曲名称" class="handle-input mr10"></el-input>
      <!-- <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button> -->
      <el-button type="primary" icon="el-icon-edit" @click="goCreate()" class="create-btn">创建</el-button>
    </div>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="name" label="歌曲名称" align="center"></el-table-column>
      <el-table-column prop="singer" label="歌手" align="center"></el-table-column>
      <el-table-column prop="createtime" label="创建时间" align="center" :formatter="dateFormat"></el-table-column>
      <el-table-column prop="updatetime" label="更新时间" align="center" :formatter="dateFormat"></el-table-column>
      <el-table-column label="操作" align="center" width="400">
        <template slot-scope="scope">
          <el-button size="small" @click="handleView(scope.$index, scope.row)">查看</el-button>
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <el-pagination
      background
      layout="prev, pager, next"
      :total="totalCount" :page-size="paramObj.limit" @current-change="handleCurrentChange">
    </el-pagination>
  </div>

</template>

<script>
import api from "@/api/api";
import { formatDate } from "@/utils/date.js";

const SUCCESS = 0;
const LIMIT = 10;
export default {
  data() {
    return {
      tableData: [],
      totalCount: 0,
      optId: "",
      paramObj: {
        name: "",
        currentPage: 1,
        limit: LIMIT
      }
    };
  },
  created() {
    this._getData();
  },
  methods: {
    dateFormat(row, column) {
      let date = row[column.property];
      if (date == undefined) {
        return "";
      }
      date = new Date(date);
      let format = "yyyy-MM-dd hh:mm:ss";
      return formatDate(date, format);
    },
    handleCurrentChange(val) {
      this.paramObj.currentPage = val;
      this._getData();
    },
    handleDelete(index, row) {
      this.optId = row.id;

      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this._sureDelete();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    _sureDelete() {
      api.delete(this.optId).then(data => {
        if (data.code == SUCCESS) {
          this._getData();
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        }
      });
    },
    _getData() {
      api.getList().then(response => {
        if (response.code == SUCCESS) {
          this.tableData = response.data.songList;
          // this.totalCount = response.data.totalElements;
        }
      });
    },
    handleView(index, row) {
      this.$router.push({ path: "/detail", query: { id: row.id } });
    },
    handleEdit(index, row) {
      this.$router.push({ path: "/edit", query: { id: row.id } });
    },
    goCreate() {
      this.$router.push({ path: "/edit" });
    },
    search() {
      // this.paramObj.currentPage = 1;
      this._getData();
    }
  }
};
</script>

<style lang="less">
.songList {
  padding: 20px;
  .handle-box {
    margin-bottom: 20px;
    .handle-select {
      width: 240px;
    }
    .handle-input {
      width: 300px;
      display: inline-block;
    }
    .create-btn {
      float: right;
      margin-right: 20px;
    }
  }
}
</style>